package com.dongdongshop.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.service.TbContentCategoryService;
import com.dongdongshop.service.TbContentService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("content")
public class ContentController {

    @DubboReference
    private TbContentService tbContentService;
    @DubboReference
    private TbContentCategoryService tbContentCategoryService;

    @RequestMapping("toContent")
    public String toContent(Model model){
        /*List<TbContentCategory> tbContentCategories = tbContentCategoryService.selectByAll();
        model.addAttribute("tbContentCategories",tbContentCategories);*/
        return "admin/content";
    }

    @RequestMapping("show")
    @ResponseBody
    public Result show(){
        List<TbContent> tbContents = tbContentService.selectAll();
        if (tbContents == null) {
            return Result.response(ResultEnum.ERROR);
        }
        return Result.response(ResultEnum.SUCCESS).setData(tbContents);
    }

    @RequestMapping("insert")
    @ResponseBody
    public Result insert(TbContent tbContent){
        boolean b = tbContentService.insert(tbContent);
        if (b){
            return Result.response(ResultEnum.SUCCESS);
        }
        return Result.response(ResultEnum.ERROR);
    }

    @RequestMapping("toupdate")
    @ResponseBody
    public Result toupdate(Long id){
        TbContent tbContent = tbContentService.selectById(id);
        List<TbContentCategory> list = tbContentCategoryService.selectByAll();
        Map<String,Object> map = new HashMap();
        map.put("content",tbContent);
        map.put("categories",list);
        if (tbContent == null) {
            return Result.response(ResultEnum.ERROR);
        }
        return  Result.response(ResultEnum.SUCCESS).setData(map);
    }

    @RequestMapping("upload")
    @ResponseBody
    public Result upload(@RequestParam("myFile") MultipartFile newsImg) {


        // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1（杭州）为例，Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
        String https = "https://";
        String endpoint = "oss-cn-beijing.aliyuncs.com";
        // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        String accessKeyId ="LTAI5tHN3yEyuvM1CgMMJaAo";
        String accessKeySecret ="tDx9LtZQpmpVhASbWq7UrGuXEtiKE0";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(https + endpoint, accessKeyId, accessKeySecret);


        String imagePath = "image/" + newsImg.getOriginalFilename();
        String buket = "dongdong220110";

        // 创建PutObjectRequest对象。
        // 依次填写Bucket名称（例如examplebucket）和Object完整路径（例如exampledir/exampleobject.txt）。Object完整路径中不能包含Bucket名称。
        PutObjectRequest putObjectRequest = null;
        try {
            putObjectRequest = new PutObjectRequest(buket, imagePath, newsImg.getInputStream());
            ossClient.putObject(putObjectRequest);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 关闭OSSClient。
        ossClient.shutdown();
        //String dowloadUrl = https + buket + "." + endpoint + "/" + imagePath;
        StringBuilder builder = new StringBuilder();
        builder.append(https).append(buket).append(".").append(endpoint).append("/").append(imagePath);
        return Result.response(ResultEnum.SUCCESS).setData(builder.toString());
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(TbContent tbContent){
        boolean b = tbContentService.updateALl(tbContent);
        if(b){
           return Result.response(ResultEnum.SUCCESS);
        }
        return Result.response(ResultEnum.ERROR);
    }


    @RequestMapping("deleteGoods")
    @ResponseBody
    public Result deleteGoods(@RequestParam("ids") Long[] ids){
        boolean b = tbContentService.deleteByIds(ids);
        if(b){
            return Result.response(ResultEnum.SUCCESS);
        }
        return Result.response(ResultEnum.ERROR);
    }
}
